<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - kotobank</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-kotobank.html">here</a>.</p>


<div id="kotobank" style="display: none;">
<b>${query}</b>でKotobankを検索:
<br/><br/>
{for item in items}
  <div style="clear: both;font-size:90%;">
    <b>${item.title}</b>
    <br/>
    <div style="margin-left: 20px; font-size: 90%;">
      ${item.doc}
      <br/>
      <A href="${item.dictionaryUrl}">${item.dictionary}</A>
    </div>
    <div style="font-size:60%;clear:both;"> <br/></div>
  </div>
{forelse}
  <b>該当項目が見つかりませんでした</b>
{/for}
    <div style="font-size:30%;clear:both;"> <br/></div>
</div>

<script class="commands"><![CDATA[

CmdUtils.CreateCommand({
    name: "kotobank",
    description: "Kotobank.jp",
    takes: {search: noun_arb_text},
    icon: "http://kotobank.jp/i/common/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "<small>「kotobank」は、朝日新聞、朝日新聞出版、講談社、小学館などの辞書、事典44冊43万語から、用語を一度に検索できるサービスです。</small>";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> を Kotobank で検索します...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        //Utils.reportInfo(getGooDicUrl(searchText));
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: getKotobankUrl(searchText),
          dataType: "html",
          
          error: function() {
            previewBlock.innerHTML = "Error searching Kotobank Dictionary.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            var items = [];
            var i = 0;
            var baseHref = "http://kotobank.jp";
            
            responseData.find(".wordExpound .wrap .block").each(function(){
                
                var resultList = jQuery(this);
                
                var title = resultList.find(".title").text();
                //var url = ;
                
                resultList.find(".full .doc a").attr("href", function(){
                    return addBaseUrl(this, baseHref);
                });
                var doc = resultList.find(".full .doc").html();
                
                var key = ++i;
                
                var item = {key : key,
                            title : title,
                            doc : doc,
                            };
                
                items.push(item);
                
            });
            
            var previewData = {
              query : searchText,
              items : items
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#kotobank", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
        Utils.openUrlInBrowser( getKotobankUrl(directObject.text) );
    }
});

function getKotobankUrl(query){
    var url = "http://kotobank.jp/search/result?q={searchTerms}&c=opensearch";
    url = url.replace(/%s|{searchTerms}/g, encodeURI(query) );
    return url;
}


function addBaseUrl(href, baseUrl){
    
    uri = parseUri(href);
    
    if(uri.host == ""){
        href = baseUrl + href;
    }else if(uri.host == "ubiquity"){
        href = baseUrl + uri.relative;
    }
    
    return href;
}

/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: true,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


]]></script>
</body>
</html>
